if (!com) var com = {};
if (!com.ryanscook) com.ryanscook = {};
if (!com.ryanscook.ecm) com.ryanscook.ecm = {};


com.ryanscook.ecm = {
    contextPopUp: function() {
        gContextMenu.showItem("cls-context-copyplain", gContextMenu.isTextSelected);
        gContextMenu.showItem("cls-context-copyhtml", gContextMenu.isTextSelected);
    },

    copyPlainText: function() {
        try {
            var objSelection = com.ryanscook.ecm.getSelection();

            com.ryanscook.ecm.addToClipboard(com.ryanscook.ecm.trimString(objSelection.toString()));
        } catch (ex) {
            alert("Extended Copy Menue Error: " + ex.message);
        }
    },

    copyHtmlText: function() {
        try {
            var objSelection = com.ryanscook.ecm.getSelection();
            var objRange = objSelection.getRangeAt(0);
            var objClone = objRange.cloneContents();
            var objDiv = window.content.document.createElement('div');

            objDiv.appendChild(objClone);

            com.ryanscook.ecm.addToClipboard(objDiv.innerHTML);
        } catch (ex) {
            alert("Extended Copy Menue Error: " + ex.message);
        }
    },

    getSelection: function() {
        var objFocusedWnd = document.commandDispatcher.focusedWindow;
        var selection = objFocusedWnd.getSelection();

        return selection;
    },

    trimString: function(str) {
        return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    },

    editMenu_OnPopupShowing: function() {
        try {
            var strSelection = com.ryanscook.ecm.trimString(com.ryanscook.ecm.getSelection().toString());

            if (strSelection == null || strSelection.length == 0) {
                document.getElementById('cls-edit-copyplain').setAttribute("disabled", true);
                document.getElementById('cls-edit-copyhtml').setAttribute("disabled", true);
            }
            else {
                document.getElementById('cls-edit-copyplain').setAttribute("disabled", false);
                document.getElementById('cls-edit-copyhtml').setAttribute("disabled", false);
            }
        } catch (ex) {
            alert("Extended Copy Menue Error: " + ex.message);
        }
    },

    addToClipboard: function(strData) {
        var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
        gClipboardHelper.copyString(strData);
    },

    init: function() {
        document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", com.ryanscook.ecm.contextPopUp, true);
    }

}



window.addEventListener("load", com.ryanscook.ecm.init, true);

